#include "GameRun.h"
#include "GameDeviceSingleton.h"
#include "ArmyEntityFactory.h"
GameRun::GameRun(GameManagement* owner)
{
	_timetick = 0;
	_owner = owner;
	// demo
	//_sprBackground = new Sprite(_owner->getSpriteHandler(), _LEVEL1_DEMO);
	_camera = new Camera(0, 7800, 2000, 1000);
	// initialize system
	_initSystem();
}

GameRun::~GameRun()
{
}

void GameRun::onChangeMap() {
	_entityManager->changeMap(GameDeviceSingleton::getInstance()->getCurrenMapLevel());
	tileManager->changeTile(GameDeviceSingleton::getInstance()->getCurrenMapLevel());
	tileManager->changeTile(GameDeviceSingleton::getInstance()->getCurrenMapLevel());
	GameDeviceSingleton::getInstance()->setMapWiHi(tileManager->width, tileManager->height);
	if (GameDeviceSingleton::getInstance()->getCurrenMapLevel() == 2) {
		viewPort->setViewPort(false);
	} else {
		viewPort->setViewPort(true);
	}
	updateDataOnChangeMap();
}

void GameRun::updateDataOnChangeMap() {
	double x,y;
	double x1,y1;
	switch (GameDeviceSingleton::getInstance()->getCurrenMapLevel()) {
	case 2:
		isSprite = true;
		x1 = x = 200;
		y = 400;
		y1 = 200;
		break;
	case 3:
		isSprite = false;
		x1 = x = 200;
		y1 = y = 400;
		break;
	default: 
		isSprite = true;
		x = 400;
		x1 = 200;
		y1 = y = 400;
	}
	_contraEntity->getData()->getRect()->setPosition(x1, y1);
	viewPort->setPosition(x, y);
}

void GameRun::draw()
{
	//_entityManager->draw(0);
	//// demo 
	if (GameDeviceSingleton::getInstance()->checkOnChangeMap()) {
		onChangeMap();
	}
	viewPort->update();
	tileManager->draw();
	// Check to draw tile sprites for demo only
	if (_timetick % 10 == 0) isSprite = !isSprite;
	if (GameDeviceSingleton::getInstance()->getCurrenMapLevel() == 1 && isSprite)
		tileSpriteManager->draw();
	if (GameDeviceSingleton::getInstance()->getCurrenMapLevel() == 2 && isSprite)
		tileSpriteManager2->draw();
	if (GameDeviceSingleton::getInstance()->getCurrenMapLevel() == 3 && isSprite)
		tileSpriteManager3->draw();

	_entityManager->draw(0);
	GameDeviceSingleton::getInstance()->getGameBullet()->draw(0);
	_contraEntity->draw(_timetick);
	GameDeviceSingleton::getInstance()->drawLogic();

	_effectManager->draw(_timetick);
}

void GameRun::update()
{
	GameDeviceSingleton::getInstance()->getGameBullet()->update(_timetick);
	_entityManager->update(_timetick);
	_timetick++;

	_contraEntity->update(_timetick);

	_effectManager->uppdate(_timetick);
}

void GameRun::_initSystem()
{
	this->init();
	//init camera
}

void GameRun::setInput(int keycode, bool onHold) {
	_gameInput->setInput(keycode, onHold);
}

GameManagement* GameRun::getParent()
{
	return _owner;
}

Camera* GameRun::getCamera()
{
	return _camera;
}

void GameRun::init() {
	viewPort = new CViewPort(0, 0, 515, 480);
	GameDeviceSingleton::getInstance()->setViewPort(viewPort);

	tileManager = new TileManager(GameLevel1);
	tileSpriteManager = new TileManager(GameLevel1Sprite);
	tileSpriteManager2 = new TileManager(GameLevel2Sprite);
	tileSpriteManager3 = new TileManager(GameLevel3Sprite);
	GameDeviceSingleton::getInstance()->setMapWiHi(tileManager->width, tileManager->height);

	_contraEntity = new ContraEntity(1, 200, 400);
	_contraEntity->getAbstractData()->initSpriteHandler(*(GameDeviceSingleton::getInstance()->getSpriteHandler()));
	_contraEntity->initData();
	GameDeviceSingleton::getInstance()->setPlayerRect(_contraEntity->getAbstractData()->getRect());
	//GameDeviceSingleton::getInstance()->setCurrentMapLevel(1);

	viewPort->setTarget(_contraEntity->getAbstractData()->getRect());

	_entityManager = new EntityManager();
	_entityManager->setContra(_contraEntity);

	_gameInput = new GameRunInput(_contraEntity, this);

	_effectManager = new EffectManager();
}